/**
 *  @(#)  IFAAAuthenticator  module    1.0  2017/03/24
 *
 *  Copyright  (c)  2017,  IFAA  and/or  its  affiliates.  All  rights  reserved.
 *
 *  Redistribution  and  use  in  source  and  binary  forms,  with  or  without
 *  modification,  are  permitted  provided  that  the  following  conditions  are  met:
 *
 *  -Redistribution  of  source  code  must  retain  the  above  copyright  notice,  this
 *    list  of  conditions  and  the  following  disclaimer.
 *
 *  -Redistribution  in  binary  form  must  reproduce  the  above  copyright  notice,
 *    this  list  of  conditions  and  the  following  disclaimer  in  the  documentation
 *    and/or  other  materials  provided  with  the  distribution.
 *
 *  Neither  the  name  of  IFAA  or  the  names  of  contributors  may
 *  be  used  to  endorse  or  promote  products  derived  from  this  software  without
 *  specific  prior  written  permission.
 *
 *  This  software  is  provided  "AS  IS,"  without  a  warranty  of  any  kind.  ALL
 *  EXPRESS  OR  IMPLIED  CONDITIONS,  REPRESENTATIONS  AND  WARRANTIES,  INCLUDING
 *  ANY  IMPLIED  WARRANTY  OF  MERCHANTABILITY,  FITNESS  FOR  A  PARTICULAR  PURPOSE
 *  OR  NON-INFRINGEMENT,  ARE  HEREBY  EXCLUDED.  IFAA,  ORG.
 *  AND  ITS  LICENSORS  SHALL  NOT  BE  LIABLE  FOR  ANY  DAMAGES  SUFFERED  BY  LICENSEE
 *  AS  A  RESULT  OF  USING,  MODIFYING  OR  DISTRIBUTING  THIS  SOFTWARE  OR  ITS
 *  DERIVATIVES.  IN  NO  EVENT  WILL  IFAA  OR  ITS  LICENSORS  BE  LIABLE  FOR  ANY  LOST
 *  REVENUE,  PROFIT  OR  DATA,  OR  FOR  DIRECT,  INDIRECT,  SPECIAL,  CONSEQUENTIAL,
 *  INCIDENTAL  OR  PUNITIVE  DAMAGES,  HOWEVER  CAUSED  AND  REGARDLESS  OF  THE  THEORY
 *  OF  LIABILITY,  ARISING  OUT  OF  THE  USE  OF  OR  INABILITY  TO  USE  THIS  SOFTWARE,
 *  EVEN  IF  IFAA  HAS  BEEN  ADVISED  OF  THE  POSSIBILITY  OF  SUCH  DAMAGES.
 *
 *  You  acknowledge  that  this  software  is  not  designed,  licensed  or  intended
 *  for  use  in  the  design,  construction,  operation  or  maintenance  of  any
 *  nuclear  facility.
 *
 */
package com.ifaa.sdk.auth;

import android.content.Context;

import com.ifaa.sdk.auth.message.AuthenticatorMessage;

/**
 * 
 * @author junsui.ljs
 * @version $Id: Abstractauthenticator.java, v 0.1 2014 5:08:54 junsui.ljs Exp $
 */
public abstract class AbstractAuthenticator implements IAuthenticator {

    /**
     * context
     */
    protected Context context = null;

    /**
     * supported
     */
    protected boolean supported = false;

    /**
     * next Handler
     */
    protected IAuthenticator next;

    /**
     * Getter method for property <tt>context</tt>.
     * 
     * @return property value of context
     */
    public Context getContext() {
        return context;
    }

    /**
     * Setter method for property <tt>context</tt>.
     * 
     * @param context value to be assigned to property context
     */
    public void setContext(Context context) {
        this.context = context;
    }

    /**
     * Getter method for property <tt>supported</tt>.
     * 
     * @return property value of supported
     */
    public boolean isSupported() {
        return supported;
    }

    /**
     * Setter method for property <tt>supported</tt>.
     * 
     * @param supported value to be assigned to property supported
     */
    public void setSupported(boolean supported) {
        this.supported = supported;
    }

    /**
     * Getter method for property <tt>next</tt>.
     * 
     * @return property value of next
     */
    public IAuthenticator getNext() {
        return next;
    }

    /**
     * Setter method for property <tt>next</tt>.
     * 
     * @param next  value to be assigned to property next
     */
    public void setNext(IAuthenticator next) {
        this.next = next;
    }

    /**
     * 
     * @see com.ifaa.sdk.auth.IAuthenticator#process(
     * com.ifaa.sdk.auth.message.AuthenticatorMessage, com.ifaa.sdk.auth.AuthenticatorCallback)
     */
    @Override
    public void process(AuthenticatorMessage request, AuthenticatorCallback authenticatorCallback) {

        switch (request.getType()) {
            case AuthenticatorMessage.MSG_REGISTER_REQUEST:
                doRegister(request, authenticatorCallback);
                break;
            case AuthenticatorMessage.MSG_AUTHENTICATOR_REQUEST:
                doAuthenticate(request, authenticatorCallback);
                break;
            case AuthenticatorMessage.MSG_DEREGISTER_REQUEST:
                doDeregister(request, authenticatorCallback);
                break;
            default:
                break;
        }
    }

    /**
     * synchronous process requestMessage
     * @param request
     * @return AuthenticatorResponse
     */
    public String process(AuthenticatorMessage request) {
        throw new UnsupportedOperationException("Synchronous process is not supported");
    }

    /**
     * prapareKeyPair 
     *
     * @return
     */
    public void prapareKeyPair() {

    }

    /**
     * Check the TEE of userid status 
     * 用户注册指纹或者是没有注册指纹或者是注册之后被删除了。
     *
     * @return
     */
    public int checkUserStatus(String userID) {
        return 0;
    }

    /**
     * Use HW sdk to see registered fp numbers
     *
     * @return
     */
    public int registedFingerPrintNumber() {
        return 0;
    }

    /**
     * doRegister
     * 
     * @param registerRequest
     * @param authenticatorCallback
     */
    protected abstract void doRegister(AuthenticatorMessage registerRequest,
                                       AuthenticatorCallback authenticatorCallback);

    /**
     * do authenticate
     * 
     * @param authenticateRequest
     * @param authenticatorCallback this handler was passed from the caller and send the onResult
     * @return
     */
    protected abstract void doAuthenticate(AuthenticatorMessage authenticateRequest,
                                           AuthenticatorCallback authenticatorCallback);

    /**
     * doderegister
     * 
     * @param deRegisterRequest
     * @param authenticatorCallback this handler was passed from the caller and send the onResult
     * @return
     */
    protected abstract void doDeregister(AuthenticatorMessage deRegisterRequest,
                                         AuthenticatorCallback authenticatorCallback);

}
